package com.baizhi.mall.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.baizhi.mall.entity.mysql.BzBrandDO;
import com.baizhi.mall.service.IBzBrandService;
import com.baizhi.mall.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhw
 * @since 2021-05-17
 */
@RestController
public class BzBrandController {
    @Value("${OSS.endpoint}")
    private String endpoint;
    @Autowired
    private OSS ossClient;
    @Autowired
    private IBzBrandService bs;
    @GetMapping("/brandAll")
    public ResultEntity<List<BzBrandDO>> selectAll(){
        List<BzBrandDO>list=bs.selectAll();
        return ResultEntity.ok(list);
    }
    @PostMapping("/brandIns")
    public ResultEntity<String> insert(@RequestBody BzBrandDO brand){
        bs.insert(brand);
        return ResultEntity.ok();
    }
    @DeleteMapping("/brand/{id}")
    public ResultEntity<String>delete(@PathVariable("id")Integer id){
        bs.delete(id);
        return ResultEntity.ok();
    }
    @PutMapping("/brandUpd")
    @PreAuthorize("hasAuthority('sys:user:update,sys:role:select')")
    public ResultEntity<String> update(@RequestBody BzBrandDO brand){
        bs.update(brand);
        return ResultEntity.ok();
    }
    @GetMapping("/status2")
    public ResultEntity<String> updateStatus(Integer showStatus,Integer brandId){
        bs.updateStatus(showStatus,brandId);
        return ResultEntity.ok();
    }
    @PostMapping("/upload")
    public ResultEntity upload(MultipartFile multipartFile) throws IOException {
        String bulkName="bz-mall";
        String key = UUID.randomUUID().toString()+multipartFile.getOriginalFilename();//文件的唯一标识，通常是不重复的文件名
        InputStream inputStream=multipartFile.getInputStream();
        PutObjectResult putObjectRequest = ossClient.putObject(bulkName,key,inputStream);
        String result="https://"+bulkName+"."+endpoint+"/"+key;
        return ResultEntity.ok(result);
    }
}

